package com.mybatis.dao;

import com.mybatis.pojo.User;
import com.mybatis.util.mybatisUtil;
import org.apache.ibatis.session.SqlSession;
import org.apache.log4j.Logger;
import org.junit.Test;

import java.util.HashMap;
import java.util.List;

//注意导包Log4j
public class userdaoText {
    static Logger logger = Logger.getLogger(userdaoText.class);
//    查询所有用户
    @Test
    public void test(){
        SqlSession sqlSession = mybatisUtil.getSqlSession();
        Mapper userdao = sqlSession.getMapper(Mapper.class);
        List<User> userList = userdao.getUserList();

        for (User user :userList){
            System.out.println(user);
        }
        sqlSession.close();
    }
//    分页查询
    @Test
    public void getUserByLimit(){
        SqlSession sqlSession = mybatisUtil.getSqlSession();
        Mapper mapper = sqlSession.getMapper(Mapper.class);
        HashMap<String, Object> map = new HashMap<String, Object>();
        map.put("startIndex",0);
        map.put("pageSize",2);
        List<User> userList = mapper.getUserByLimit(map);
        for (User user : userList) {
            System.out.println(user);
        }

        sqlSession.close();
    }
//    根据ID查询
    @Test
    public void  getUserById(){
        SqlSession sqlSession = mybatisUtil.getSqlSession();
        Mapper mapper =  sqlSession.getMapper(Mapper.class);
        User user = mapper.getUserId(1);
        System.out.println(user);
        sqlSession.close();


    }
//  添加用户
    @Test
    public void addUser(){
        SqlSession sqlSession = mybatisUtil.getSqlSession();
        Mapper mapper =  sqlSession.getMapper(Mapper.class);
        int in=mapper.addUser(new User(9,"图图","123456"));
        if(in>0){
            System.out.println("插入数据成功");
        }
        sqlSession.commit();
        sqlSession.close();
    }

//删除一名用户
    @Test
    public void deletUser(){
        SqlSession sqlSession = mybatisUtil.getSqlSession();
        Mapper mapper = sqlSession.getMapper(Mapper.class);
        int i = mapper.deletUser(9);
        if (i>0){
            System.out.println("删除成功");
        }
        sqlSession.commit();
        sqlSession.close();
    }
//修改用户名
    @Test
    public void UpdateUser(){
        SqlSession sqlSession = mybatisUtil.getSqlSession();
        Mapper mapper = sqlSession.getMapper(Mapper.class);
        int in = mapper.updateUser(new User(1,"图图","123456"));
        if (in>0){
            System.out.println("修改成功");
        }
        sqlSession.commit();
        sqlSession.close();
    }

}
